import {useEffect, useState} from "react";
import {DetailRes, fetchDetailApi} from "@/apis/detail";

function useDetail(id: string) {
    // 文章详情
    const [detail, setDetail] = useState<DetailRes | null>(null);

    // 渲染后执行
    useEffect(() => {
        // 定义发送请求函数
        const getDetail = async () => {
            try {
                const res = await fetchDetailApi(id!);
                setDetail(res.data.data)
            } catch (e) {
                throw new Error("fetch Detail Error")
            }

        }
        getDetail()
        // 执行函数
    }, [id])

    return {
        detail
    }
}

export {useDetail}